import os
modpath = "D:\\Documents\\Paradox Interactive\\Crusader Kings III\\mod\\character\\"
# put your folder after \\props if you created a new one in \props\
assetpath = "gfx\\models\\portraits\\props\\"
# sub folder under assetpath, put it empty if you don't use
dir = "BangDream"
trait_prefix = "BangDream_"
mod_prefix = "MyGO_"

assetpath = os.path.join(assetpath, dir)
# accessories = "resource\\accessories"
accessories = "gfx\\portraits\\accessories"
# genes = "resource\\genes"
gene = "common\\genes"
# portraits = "resource\\portrait_modifiers"
portraits = "gfx\\portraits\\portrait_modifiers"
# optional, if you have more than 1 portrait for a character.
trigger_file = "common\\scripted_triggers\\"+mod_prefix+"portrait_triggers.txt"
portrait_dict = dict()
code = "utf_8_sig"    # utf8-bom, paradox coding


def main_handler():
    currentpath = os.path.join(modpath, assetpath)
    dir = currentpath.split("\\")[-1]
    output_portraits = open(os.path.join(modpath, portraits, dir+"_portraits"+".txt"), "w", encoding=code)
    output_portraits.write(dir+"_portrait = {\n\tportrait_group = anime")
    ddslist = os.listdir(currentpath)
    output_asset = open(os.path.join(currentpath, "00_"+mod_prefix+dir+".asset"), "w", encoding="utf_8_sig")
    output_props = open(os.path.join(modpath, accessories, dir+"_props.txt"), "w", encoding=code)
    output_accessories = open(os.path.join(modpath, gene, dir+"_genes_special_accessories.txt"), "w", encoding=code)
    output_accessories.write("special_genes = {\n\taccessory_genes = {\n\t\tportrait_group = anime")
    output_nums = open(os.path.join(modpath, trigger_file), "w", encoding=code)

    id = 0
    slices = 1
    propgroup = dir+'_props_'+str(slices)
    output_accessories.write('\n\t\t'+propgroup+' = {\n\t\t\tgroup = anime_portrait_group')
    for file in ddslist:
        if os.path.isfile(os.path.join(currentpath, file)):
            if file[-12:] == '_diffuse.dds':
                name = file[:-12]
                id = id + 1
                if id == 256:
                    id = 1
                    slices += 1
                    propgroup = dir+'_props_'+str(slices)
                    output_accessories.write('\n\t\t}\n\t\t'+propgroup+' = {\n\t\t\tgroup = anime_portrait_group')
                if name[-7:] == '_weapon':
                    protraits_type = name[-6:]
                    skinindex = int(name.split("_")[-2])
                    shortname = name[:-1-int(len(str(skinindex)))-7]
                else:
                    protraits_type = 'DeFault'
                    skinindex = int(name.split("_")[-1])
                    shortname = name[:-1-int(len(str(skinindex)))]
                if skinindex != 0 and skinindex != 99 and (shortname not in portrait_dict or portrait_dict[shortname] < skinindex):
                    portrait_dict[shortname] = skinindex
                output_portraits.write('''
\t'''+name+''' = {
\t\tdna_modifiers = {
\t\t\taccessory = {
\t\t\t\tmode = add
\t\t\t\tgene = '''+propgroup+'''
\t\t\t\ttemplate = '''+name+'''
\t\t\t\tvalue = 1
\t\t\t}
\t\t}
\t\tweight = {
\t\t\tbase = 0
\t\t\tmodifier = {
\t\t\t\tadd = 200
\t\t\t\t''')
                output_portraits.write('has_trait = '+shortname)
                if skinindex == 0:
                    output_portraits.write('''\n\t\t\t\tshow_default_portrait = yes''')
                else:
                    output_portraits.write("\n\t\t\t\tvar:portrait_index ?= "+str(skinindex))
                output_portraits.write('''
\t\t\t\tnormal_portrait_blocked_trigger = no''')
                if protraits_type == 'weapon' :
                     output_portraits.write('''\n\t\t\t\tportrait_wear_armor_trigger = yes''')
                output_portraits.write('''
\t\t\t}
\t\t}
\t}''')
                output_accessories.write('''
\t\t\t'''+name+''' = {
\t\t\t\tindex = '''+str(id)+'''
\t\t\t\tanime_male = { 1 = '''+name+''' }
\t\t\t\tanime_female = anime_male
\t\t\t\tanime_boy = anime_male
\t\t\t\tanime_girl = anime_male
\t\t\t}''')
                output_props.write(name+' = { portrait_group = anime entity = { required_tags = "" node = "bn_h_head_mid" entity = "'+name+'_entity" } }\n')
                output_asset.write('''pdxmesh = {
\tname = "'''+name+'''_mesh"
\tfile = "hm_prophet.mesh"
\tscale = 1.6
\tmeshsettings = {
\t\tname = "prophet_shieldShape"
\t\tindex = 0
\t\ttexture_diffuse = "'''+name+'''_diffuse.dds"
\t\ttexture_specular = "'''+name+'''_diffuse.dds"
\t\tshader = "portrait_attachment_alpha_to_coverage"
\t\tshader_file = "gfx/hmportrait.shader"
\t}
}
entity = {
\tname = "'''+name+'''_entity"
\tpdxmesh = "'''+name+'''_mesh"
}
''')
                lastindex = skinindex
    output_accessories.write("\n\t\t}\n\t}\n}\n")
    output_asset.close()
    output_props.close()
    output_accessories.close()
    output_portraits.write("\n}\n")
    output_portraits.close()

    # print(portrait_dict)
    maxportrait = 0
    for item in portrait_dict.items():
        if maxportrait < item[1]:
            maxportrait = item[1]
    # print("max portrait = ", maxportrait)
    for i in range(maxportrait, 0, -1):
        output_nums.write(trait_prefix+"portrait_num_"+str(i)+" = {\n\tOR = {")
        flag = 0
        for item in portrait_dict.items():
            if item[1] == i:
                output_nums.write("\n\t\thas_trait = "+item[0])
                flag = 1
        if flag == 0:
            output_nums.write("\n\t\talways = no")
        output_nums.write("\n\t}\n}\n")
    output_nums.close()


main_handler()
